use ffmpeg_sys_next::{
    AVCodec, AVCodecContext, AVCodecID, AVCodecParameters, AVDictionary, AVFormatContext, AVFrame,
    AVPacket, av_frame_alloc, av_frame_free, av_packet_alloc, av_packet_free, av_packet_move_ref,
    av_packet_ref, av_packet_unref, av_read_frame, avcodec_alloc_context3, avcodec_close,
    avcodec_find_decoder, avcodec_flush_buffers, avcodec_open2, avcodec_parameters_to_context,
    avcodec_receive_frame, avcodec_send_packet,
};
use std::ffi::c_int;

pub mod decoder;
pub mod encoder;

pub fn avcodec_alloc_context3_safe(codec: *const AVCodec) -> *mut AVCodecContext {
    unsafe { avcodec_alloc_context3(codec) }
}

pub fn avcodec_parameters_to_context_safe(
    codec: *mut AVCodecContext,
    par: *const AVCodecParameters,
) -> c_int {
    unsafe { avcodec_parameters_to_context(codec, par) }
}

pub fn avcodec_open2_safe(
    avctx: *mut AVCodecContext,
    codec: *const AVCodec,
    options: *mut *mut AVDictionary,
) -> c_int {
    unsafe { avcodec_open2(avctx, codec, options) }
}

pub fn avcodec_find_decoder_safe(id: AVCodecID) -> *const AVCodec {
    unsafe { avcodec_find_decoder(id) }
}

pub fn av_packet_alloc_safe() -> *mut AVPacket {
    unsafe { av_packet_alloc() }
}

pub fn av_read_frame_safe(s: *mut AVFormatContext, pkt: *mut AVPacket) -> c_int {
    unsafe { av_read_frame(s, pkt) }
}

pub fn av_packet_unref_safe(pkt: *mut AVPacket) {
    unsafe { av_packet_unref(pkt) }
}

pub fn av_packet_ref_safe(dst: *mut AVPacket, src: *const AVPacket) -> c_int {
    unsafe { av_packet_ref(dst, src) }
}

pub fn avcodec_close_safe(avctx: *mut AVCodecContext) -> c_int {
    unsafe { avcodec_close(avctx) }
}

pub fn avcodec_flush_buffers_safe(avctx: *mut AVCodecContext) {
    unsafe { avcodec_flush_buffers(avctx) }
}

pub fn avcodec_send_packet_safe(avctx: *mut AVCodecContext, avpkt: *const AVPacket) -> c_int {
    unsafe { avcodec_send_packet(avctx, avpkt) }
}

pub fn avcodec_receive_frame_safe(avctx: *mut AVCodecContext, frame: *mut AVFrame) -> c_int {
    unsafe { avcodec_receive_frame(avctx, frame) }
}

pub fn av_packet_move_ref_safe(dst: *mut AVPacket, src: *mut AVPacket) {
    unsafe { av_packet_move_ref(dst, src) }
}

pub fn av_frame_alloc_safe() -> *mut AVFrame {
    unsafe { av_frame_alloc() }
}

pub fn av_packet_free_safe(pkt: *mut *mut AVPacket) {
    unsafe {
        av_packet_free(pkt);
    }
}

pub fn av_frame_free_safe(frame: *mut *mut AVFrame) {
    unsafe { av_frame_free(frame) }
}

